﻿<%@ WebHandler Language="C#" Class="PostFile" %>

using System;
using System.Web;
using System.Data;
using System.IO;
using System.Web.SessionState;

public class PostFile : IHttpHandler,IRequiresSessionState {

    public void ProcessRequest(HttpContext context)
    {
        string output = "";
        string action = context.Request["type"].ToString();
        switch (action)
        {
            case "get":
                {
                    DataTable dt = MojoCube.Office.Sql.SqlQueryDS("select * from Resume_Image where Number='" + context.Request["recId"].ToString() + "'").Tables[0];

                    if (dt.Rows.Count > 0)
                    {
                        output += "[";

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            output += "{\"id\":\"" + dt.Rows[i]["pk_Image"].ToString() + "\",\"filepath\":\"/Files/" + dt.Rows[i]["FilePath"].ToString() + "\",\"filename\":\"" + dt.Rows[i]["FileName"].ToString() + "\"},";
                        }

                        output += "]";
                    }
                    else
                    {
                        output += "[]";
                    }
                }
                break;
            case "add":
                {
                    string userId = context.Session["UserID"].ToString();

                    //文件保存目录路径
                    String savePath = "../Files/Resume/" + userId;

                    String dirPath = context.Server.MapPath(savePath);

                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }

                    HttpPostedFile imgFile = context.Request.Files["file"];
                    if (imgFile != null)
                    {
                        string ext = imgFile.FileName;
                        ext = ext.Substring(ext.LastIndexOf('.') + 1);
                        string filename = MojoCube.Api.Text.Function.DateTimeString(true);
                        imgFile.SaveAs(dirPath + "\\" + filename + "." + ext);

                        MojoCube.Office.Resume.Image image = new MojoCube.Office.Resume.Image();
                        image.fk_Resume = 0;
                        image.Number = context.Request["recId"].ToString();
                        image.FileName = Guid.NewGuid().ToString();
                        image.FilePath = "Resume/" + userId + "/" + filename + "." + ext;
                        image.FileType = ext;
                        image.FileSize = imgFile.ContentLength;
                        image.Width = 0;
                        image.Height = 0;
                        image.Title = imgFile.FileName;
                        image.SortID = 0;
                        image.Visible = true;
                        image.CreateDate = DateTime.Now.ToString();
                        image.CreateUserID = int.Parse(userId);
                        image.ModifyDate = DateTime.Now.ToString();
                        image.ModifyUserID = 0;
                        image.InsertData();
                    }
                }
                break;
            case "delete":
                {
                    string userId = context.Session["UserID"].ToString();

                    MojoCube.Office.Sql.SqlQuery("delete from Resume_Image where FileName='" + context.Request["fileName"].ToString() + "' and Number='" + context.Request["recId"].ToString() + "' and CreateUserID=" + userId);
                }
                break;
            default:
                {

                }
                break;
        }

        context.Response.ContentType = "text/plain";
        context.Response.Write(output);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}